import { Sequelize } from 'sequelize-typescript';

export interface DbConfigInterface { 
    dialect?: 'postgres' | 'mysql' | 'sqlite',
    username: string;
    password: string;
    database: string;
    sync?: boolean;
}

export default async function init(dbConfig: DbConfigInterface) { 
    const sync = dbConfig.sync || false;
    const config = Object.assign({
        dialect: 'sqlite',
        database: '',
        username: '',
        password: '',
        modelPaths: [`${__dirname}/*.model.ts`],
        modelMatch: (filename: string, member: string) => {
            console.log(filename);
            return /\.model(\.ts|\.js)/.test(filename)
        },
        operatorsAliases: false,
    }, dbConfig);
    console.log(config);
    const sequelize = new Sequelize(config);
    return sequelize.sync({ force: sync });
}
